<?php
FLEA::loadClass('FLEA_Db_TableDataGateway');

class	Model_Firm	extends		FLEA_Db_TableDataGateway
{
	var		$tableName		=	'firm';
	var		$primaryKey		=	'FirmID';

	function	findFirm($firmID)
	{
		$sql	=
				"
				select firm.* , user.Email  , user.WorkPhone , user.HomePhone , user.CellPhone,user.BService
				from user,firm
				where firm.UserID=user.UserID and firm.FirmID=".$firmID;
		$firms	=	$this->findBySql($sql);
		return $firms[0];
	}

	function	findAllFirms()
	{
			
		$sql	=
				"
				select firm.* , user.Email  , user.WorkPhone , user.HomePhone , user.CellPhone,user.BService
				from user,firm
				where firm.UserID=user.UserID";
		$firms	=	$this->findBySql($sql);
		return $firms;
	}

	function	findAllInventionFirms()
	{
		$sql	=
				"
				SELECT firm. * , user.Email, user.WorkPhone, user.HomePhone, user.CellPhone
				FROM user, firm, orderauction
				WHERE firm.UserID = user.UserID
				AND orderauction.FirmID = firm.FirmID and orderauction.Type=0
				Order by orderauction.ID asc";
		$firms	=	$this->findBySql($sql);
		return $firms;
	}

	function	findAllTradeMarkFirms()
	{
		$sql	=
				"
				SELECT firm. * , user.Email, user.WorkPhone, user.HomePhone, user.CellPhone
				FROM user, firm, orderauction
				WHERE firm.UserID = user.UserID
				AND orderauction.FirmID = firm.FirmID and orderauction.Type=1
				Order by orderauction.ID asc";
		$firms	=	$this->findBySql($sql);
		return $firms;
	}

	function	findAllCopyrightFirms()
	{
		$sql	=
				"
				SELECT firm. * , user.Email, user.WorkPhone, user.HomePhone, user.CellPhone
				FROM user, firm, orderauction
				WHERE firm.UserID = user.UserID
				AND orderauction.FirmID = firm.FirmID and orderauction.Type=2
				Order by orderauction.ID asc";
		$firms	=	$this->findBySql($sql);
		return $firms;
	}

	function	findAllFirmsWork($clientID=0)
	{
			
		$sql	=
				"
				select firm.* , user.Email  , user.WorkPhone , user.HomePhone , user.CellPhone
				from user,firm
				where firm.UserID=user.UserID and firm.FirmID in 
				(
					select invention.FirmID from invention where invention.ClientID=".$clientID."
				)";
		$firms	=	$this->findBySql($sql);
		return $firms;
	}

	function	deleteFirm($firmID)
	{
		$dbo	=	FLEA::getDBO();
			
		$sql	=	"delete from user where UserID in (select UserID from firm where FirmID=".$firmID.")";
		$dbo->execute($sql);
			
		$sql	=	"delete from firm where FirmID=".$firmID;
		$dbo->execute($sql);
			
		$sql	=	"update invention set FirmID=0 where FirmID=".$firmID;
		$dbo->execute($sql);
			
		$sql	=	"delete from practitioner where FirmID=".$firmID;
		$dbo->execute($sql);
		return 1;
	}
	/**
	 * save firm information
	 *
	 * @param unknown_type $firm
	 * @return unknown
	 */
	function	editInformation($firm)
	{
		$count	=	$this->findCount('Name',$firm['Name']);
		if($count >= 2)
		{
			return -1;
		}
		else
		{
			if ( $this->save($firm) )
			{
				return 1;
			}
			else
			{
				return 0;
			}
		}
	}
	function	getClients($firmID)
	{
		$_modelInvention	=&	FLEA::getSingleton('Model_Invention');
		$_modelClient		=&	FLEA::getSingleton('Model_Client');
		$inventions			=	$_modelInvention->findAll('FirmID='.$firmID);
		for($i=0; $i<count($inventions); $i++)
		{
			$client	=	$_modelClient->getClient($inventions[$i]['ClientID']);
			$clients[$i]	=	$client;
		}
		return $clients;
	}

	function	updateFirms($firms,$type)
	{
		$dbo	=&	FLEA::getDBO();
		
		if($firms != '')
		{
			switch($type)
			{
				case 0:
					$sql	=	"update firm set BAuction=1 ";
					$sql1	=	"update firm set BAuction=0 ";
					break;
				case 1:
					$sql	=	"update firm set BTradeMarkAuction=1 ";
					$sql1	=	"update firm set BTradeMarkAuction=0 ";
					break;
				case 2:
					$sql	=	"update firm set BCopyrightAuction=1 ";
					$sql1	=	"update firm set BCopyrightAuction=0 ";
					break;
			}
			$sql	.=	" where FirmID in (".$firms.") ";
			$sql1	.=	" where FirmID not in (".$firms.") ";
			$this->execute($sql);
			$this->execute($sql1);
			
				
			$sql	=	"delete from orderauction where Type=".$type;
			$dbo->execute($sql);
			$firmsID	=	split(',',$firms);
			
			if(is_array($firmsID))
			{
				for($i=0; $i<count($firmsID); $i++)
				{
					$sql	=	"  insert into orderauction(ID,FirmID,Type) values(".$i.",".$firmsID[$i].",".$type.")";
					$dbo->execute($sql);
				}
			}
				
			return 1;
		}
		else
		{
			switch($type)
			{
				case 0:
					$sql	=	"update firm set BAuction=0 ";
					break;
				case 1:
					$sql	=	"update firm set BTradeMarkAuction=0 ";
					break;
				case 2:
					$sql	=	"update firm set BCopyrightAuction=0 ";
					break;
			}
			$dbo->execute($sql);
			
			$sql	=	"delete from orderauction where Type=".$type;
			$dbo->execute($sql);
			return 1;
		}
	}
	function	addPractitioner($user,$practitioner,$message)
	{
		$_modelUser			=&	FLEA::getSingleton('Model_User');
		$_modelPractitioner	=&	FLEA::getSingleton('Model_Practitioner');
			
		$user1	=	$_modelUser->findByField('Email',$user['Email']);
		if($user1['UserID'] != '')
		{
			/**
			 * the email is used,try other
			 */
			return -1;
		}
			
			
			
		$user['Password']	=	$user['Email'];

		$userID				= 	$_modelUser->create($user);

		$practitioner['UserID']		=	$userID;

			
		$result	=	$_modelPractitioner->create($practitioner);

		if($result)
		{
			$firm	=	$this->findFirm($practitioner['FirmID']);
			require("Extra/Mail.php");
			set_time_limit(100);

			$SmtpEmailTo 	= $user['Email'];

			$mailsubject 	= 	"[IP Manager] ------";
			$mailHeader		=	"Content-Type:text/HTML ";

			if($message == "")
			{
				$mailBody		=	"<div style='font-family: \"Times New Roman\", Times, serif;color:#000000;font-size:14px;
						font-weight:800;'>
						You are registered to access the IP Manager site for ".$firm['Name'].".<br><br> 
						This email address is your userid.  Login here using your email address as the password.</div>";
			}
			else
			{
				$mailBody		=	"<div style='font-family: \"Times New Roman\", Times, serif;color:#000000;font-size:14px;
						font-weight:800;'>".$message."</div>";
			}

			$smtp = new smtp(SmtpServer,Smtpserverport,true,SmtpUser,SmtpPass);
			$smtp->debug = true;


			$re				=
			$smtp->sendmail($SmtpEmailTo,SmtpUserMail,$mailsubject,$mailBody,"","","",$mailHeader);
				
			if($re)
			{
				return 1;
			}
			else
			{
				return 2;
			}
		}
			
		return ($result>0)?1:0;
	}
}
?>